package site.user.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import modules.recruit.entity.ApplyPlan;
import modules.recruit.entity.ShareholderC;
import modules.recruit.service.ApplyPlanService;
import modules.recruit.service.ShareholderCService;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.guinea.core.service.EntityService;
import com.guinea.core.utils.StringUtils;
import com.guinea.core.web.StrutsAjaxAction;

public class ShareholderCActionSite extends StrutsAjaxAction {
	private ShareholderCService shareholderCService;

	private ApplyPlanService applyPlanService;

	private ApplyPlan applyPlan;

	private Long applyPlanId;

	public EntityService getEntityService() {
		return shareholderCService;
	}

	public String entity2json(Object object) {
		ShareholderC shareholderC = (ShareholderC) object;
		String result = "";
		result += "{";
		result += "\"id\" : \""
				+ (shareholderC.getId() == null ? "" : shareholderC.getId()
						.longValue()
						+ "") + "\"";
		result += ", \"equityId\" : \""
				+ (shareholderC.getPlan() == null ? "" : shareholderC
						.getPlan().getId().longValue()
						+ "") + "\"";
		result += ", \"shareholderName\" : \""
				+ StringUtils.defaultString(shareholderC.getShareholderName())
				+ "\"";
		result += ", \"stake\" : \""
				+ StringUtils.defaultString(shareholderC.getStake()) + "\"";
		result += ", \"percent\" : \""
				+ StringUtils.defaultString(shareholderC.getPercent()) + "\"";
		result += "}";
		return result;
	}
	
	protected String saveWrapper(String text){
		return "<script>window.parent.successShareholderC('"+text+"')</script>";
	}

	public ShareholderCService getShareholderCService() {
		return shareholderCService;
	}

	public void setShareholderCService(ShareholderCService shareholderCService) {
		this.shareholderCService = shareholderCService;
	}

	public ApplyPlanService getApplyPlanService() {
		return applyPlanService;
	}

	public void setApplyPlanService(ApplyPlanService applyPlanService) {
		this.applyPlanService = applyPlanService;
	}
	
	/**
	 * 初始化业务对象的函数.
	 */
	protected void findEntity(ActionForm form, HttpServletRequest request) {
		super.findEntity(form,request);
		applyPlanId = (Long) getEntityId(request, "applyPlanId", Long.class);
		if (applyPlanId != null) {
			applyPlan = getApplyPlanService().getApplyPlan(applyPlanId);
		}
	}
	
	protected void initProperties() {
		super.initProperties();
		applyPlanId = null;
		applyPlan = null;
	}

	protected boolean validateApplyPlan(HttpServletRequest request) {
		if (applyPlan == null) {
			return valid = false;
		}
		return true;
	}
	
	public ActionForward ajaxListValidator(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		initFirst(form,request);
		if (!validateApplyPlan(request))
			return handleApplyPlanMissing(mapping, request, response);
		return null;
	}
	
	/**
	 * 取得SkoruShareholder对象列表
	 */
	public ActionForward ajaxList(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		List list = getShareholderCService().getByApplyPlan(applyPlanId);
		String result = collection2json(list);
		renderText(response, "{\"data\":"+result+"}");
		return null;
	}
	
	public ActionForward ajaxSaveValidator(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		initFirst(form,request);
		if (!validateApplyPlan(request))
			return handleApplyPlanMissing(mapping, request, response);
		if (!validateForm(mapping, form, request))
			return handleFormErrors(mapping, form, request, response);
		return null;
	}
	
	protected void doInitEntity(HttpServletRequest request, ActionForm form) {
		super.doInitEntity(request, form);
		if(isCreateOperation){
			ShareholderC shareholderC = (ShareholderC) entity;
			shareholderC.setPlan(applyPlan);
			entity = shareholderC;
		}
	}

	protected ActionForward handleApplyPlanMissing(ActionMapping mapping,
			HttpServletRequest request, HttpServletResponse response) {
		renderText(response, "{\"error\":\"找不到投资规划记录!\"}");
		return null;
	}

}
